package com.study.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.exception.BusinessException;
import com.study.exception.ErrorCode;
import com.study.exception.ThrowUtils;
import com.study.model.dto.topic.TopicAddRequest;
import com.study.model.dto.topic.TopicQueryRequest;
import com.study.model.dto.topic.TopicUpdateRequest;
import com.study.model.vo.TopicVO;
import com.study.service.TopicService;
import com.study.model.entity.Topic;
import com.study.mapper.TopicMapper;
import com.study.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author YS
* @description 针对表【topic(话题)】的数据库操作Service实现
* @createDate 2025-02-18 14:31:12
*/
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic>
    implements TopicService {
    @Autowired
    private UserService userService;

    /**
     * 创建话题
     *
     * @param topicAddRequest
     * @param request
     * @return
     */
    @Override
    public Long createLearningNote(TopicAddRequest topicAddRequest, HttpServletRequest request) {
        if (topicAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (topicAddRequest.getDescription() == null) {
            topicAddRequest.setDescription("无话题描述");
        }
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicAddRequest, topic);
        topic.setAdminId(userService.getLoginUser(request).getId());
        boolean save = this.save(topic);
        if (!save) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "创建失败");
        }
        return topic.getId();
    }



    /**
     * 根据id查询话题包装类
     *
     * @param id
     * @return
     */
    @Override
    public TopicVO getTopicVOById(long id) {
        Topic topic = this.baseMapper.selectById(id);
        ThrowUtils.throwIf(topic == null, ErrorCode.NOT_FOUND_ERROR);
        TopicVO topicVO = new TopicVO();
        BeanUtils.copyProperties(topic, topicVO);
        return topicVO;

    }

    /**
     * 更新话题
     *
     * @param topicUpdateRequest
     * @return
     */
    @Override
    public Boolean updateTopic(TopicUpdateRequest topicUpdateRequest) {
        if (topicUpdateRequest == null || topicUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Topic topic = this.baseMapper.selectById(topicUpdateRequest.getId());
        BeanUtils.copyProperties(topicUpdateRequest, topic);
        return this.updateById(topic);
    }

    /**
     * 分页获取话题封装类
     *
     * @param topicQueryRequest
     * @return
     */
    @Override
    public Page<TopicVO> listTopicVOByPage(TopicQueryRequest topicQueryRequest) {
        if (topicQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = topicQueryRequest.getCurrent();
        long size = topicQueryRequest.getPageSize();
        Page<Topic> notePage = page(new Page<>(current, size),getQueryWrapper(topicQueryRequest));
        Page<TopicVO> noteVOPage= new Page<>(current, size, notePage.getTotal());
        List<TopicVO> noteVOList = getTopicVoList(notePage.getRecords());
        noteVOPage.setRecords(noteVOList);
        return noteVOPage;
    }

    /**
     * 获取话题VO列表
     * @param topicList
     * @return
     */
    private List<TopicVO> getTopicVoList(List<Topic> topicList) {
        if (CollUtil.isEmpty(topicList)) {
            return new ArrayList<>();
        }
        return topicList.stream().map(this::getTopicVO).collect(Collectors.toList());
    }

    private TopicVO getTopicVO(Topic topic) {
        if(topic == null){
            return null;
        }
        TopicVO topicVO = new TopicVO();
        BeanUtils.copyProperties(topic, topicVO);
        return topicVO;
    }

    /**
     * 获取话题分页QueryWrapper
     * @param topicQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Topic> getQueryWrapper(TopicQueryRequest topicQueryRequest) {
        if (topicQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = topicQueryRequest.getId();
        Long adminId = topicQueryRequest.getAdminId();
        String title = topicQueryRequest.getTitle();
        String description = topicQueryRequest.getDescription();
        String sortField = topicQueryRequest.getSortField();
        String sortOrder = topicQueryRequest.getSortOrder();


        QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjUtil.isNotNull(id), "id", id);
        queryWrapper.eq(ObjUtil.isNotNull(adminId), "adminId", adminId);
        queryWrapper.like(StrUtil.isNotBlank(title), "title", title);
        queryWrapper.like(StrUtil.isNotBlank(description), "description", description);
        queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), sortOrder.equals("ascend"), sortField);
        return queryWrapper;
    }
}




